<?php
/**
 * Address_book_model
 * 地址簿
 *
 * @uses TXT_Model
 * @package
 * @copyright 2003-2011 The PHP Developer
 * @author Smiling Dolphin <dolphin@php5x.com>
 * @license PHP Version 5.2 {@link http://www.php.net/}
 * @version $id$
 */

class Address_book_model extends TXT_Model {

    /**
     * 状态：普通
     */
    const STATUS_NORMAL = 1;

    /**
     * 状态：删除
     */
    const STATUS_REMOVE = 9;

    /**
     * _table 
     * 表名
     * 
     * @var string
     * @access private
     */
    private $_table = 'address_books';

    /**
     * __construct
     * 构造函数
     *
     * @access protected
     * @return void
     */
    function __construct() {

        parent::__construct();
        $this->load->database('default');
        $this->load->library('session');
    }

    /**
     * addAddressBook 
     * 添加地址簿
     * 
     * @param integer $cgid 客户组ID
     * @access public
     * @return integer
     */
    function addAddressBook($cgid) {

        // 如果不是POST，返回false
        if (!$this->input->is_post()) {
            return false;
        }

        // 接收外部数据
        $uid = $this->session->userdata('uid');
        $abMobile = $this->input->post('ab_mobile');
        $abName = $this->input->post('ab_name');
        $abProperty = $this->input->post('ab_property');

        if (!$cgid || !$uid || !$abMobile) {
            show_error('Expectations of the necessary parameters', 500);
            return false;
        }

        if ($this->_isMobileExistsByUidAndAbMobile($uid, $abMobile)) {
            show_error('mobile is exists', 500);
            return false;
        }

        $insertData = array(
            'cgid' => $cgid,
            'uid' => $uid,
            'ab_mobile' => $abMobile,
            'ab_name' => $abName,
            'ab_property' => $abProperty,
            'ab_status' => self::STATUS_NORMAL,
            'ab_created' => date($this->config->item('log_date_format'))
        );

        $result = $this->db->insert($this->_table, $insertData);

        if ($result) {
            $this->load->model('Customer_group_model');
            $this->Customer_group_model->incrCount($cgid);
        }

        return $result;
    }

    /**
     * editAddressBook 
     * 编辑地址簿
     * 
     * @param integer $abid 地址簿ID
     * @access public
     * @return boolean
     */
    function editAddressBook($abid) {

        // 如果不是POST，返回false
        if (!$this->input->is_post()) {
            return false;
        }

        // 判断uid参数
        if (!$abid) {
            return false;
        }

        // 接收表单数据
        $uid = $this->session->userdata('uid');
        $cgid = $this->input->post('cgid');
        $abMobile = $this->input->post('ab_mobile');
        $abName = $this->input->post('ab_name');
        $abProperty = $this->input->post('ab_property');

        // 更新的用户数据
        $updateData = array(
            'cgid' => $cgid,
            'ab_mobile' => $abMobile,
            'ab_name' => $abName,
            'ab_property' => $abProperty,
            'cg_updated' => date($this->config->item('log_date_format'))
        );

        return $this->db->update($this->_table, $updateData, array('abid' => $abid, 'uid' => $uid));
    }

    /**
     * delete 
     * 删除地址簿
     * 
     * @param integer $abid 地址簿ID
     * @access public
     * @return boolean
     */
    function delete($abid) {

        $deleteData = array(
            'ab_status' => self::STATUS_REMOVE,
            'ab_deleted' => date($this->config->item('log_date_format'))
        );

        return $this->db->update($this->_table, $deleteData, array('abid' => $abid));
    }

    /**
     * listAddressBookByUid 
     * 根据用户ID列出地址簿
     * 
     * @param integer $uid 用户ID
     * @access public
     * @return array
     */
    function listAddressBookByUid($uid) {

        $query = $this->db->get_where($this->_table, array('uid' => $uid, 'ab_status !=' => self::STATUS_REMOVE));
        $data = array();

        foreach($query->result_array() as $row) {
            $data[] = $row;
        }

        return $data;
    }

    /**
     * listAddressBookByCgid 
     * 根据用户ID列出地址簿
     * 
     * @param integer $cgid 组ID
     * @access public
     * @return array
     */
    function listAddressBookByCgid($cgid) {

        $query = $this->db->get_where($this->_table, array('cgid' => $cgid, 'ab_status !=' => self::STATUS_REMOVE));
        $data = array();

        foreach($query->result_array() as $row) {
            $data[] = $row;
        }

        return $data;
    }

    /**
     * _isMobileExistsByUidAndAbMobile 
     * 该手机号在该用户下是否存在
     * 
     * @param integer $uid 用户ID
     * @param string $abMobile 手机号码
     * @access private
     * @return boolean
     */
    private function _isMobileExistsByUidAndAbMobile($uid, $abMobile) {

        $query = $this->db->get_where($this->_table, array('uid' => $uid,
            'ab_mobile' => $abMobile,
            'ab_status !=' => self::STATUS_REMOVE));

        if ($query->num_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }
}
